#!/usr/bin/env python
#coding:utf-8

import Winio
import threading

class PCL730:
    def __init__(self, baseaddr):
        self._baseaddr = baseaddr
        self.lock = threading.Lock()
        self.val = 0x0000
        self.WriteDO(0x0000)
        
    def ReadDI(self):
        val, res = Winio.GetPortVal(self._baseaddr, 2)
        return val
    
    def WriteDO(self, value):
        if self.val == value:
            return
        if Winio.SetPortVal(self._baseaddr, value, 2):
            print self._baseaddr, value
            self.val = value
            
    def WriteDOBit(self, value, index):
        with self.lock:
            v = self.val
            if value:
                v |= (0x0001 << index)
            else:
                v &= ~(0x0001 << index)
            print v
            self.WriteDO(v)
        
    def ReadDO(self):
        return self.val

if __name__ == '__main__':
    Winio.InitializeWinIo()
    pcl730 = PCL730(0xc800)
    pcl730.WriteDOBit(1, 2)
    Winio.ShutdownWinIo()
